# @Time : 2020/8/31 16:12
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import imutils
from matplotlib import pyplot as plt
image = cv.imread("pic/28.bmp")
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)

eq = cv.equalizeHist(gray)

grayShow = cv.resize(gray,(gray.shape[1] // 6,gray.shape[0] // 6),interpolation=cv.INTER_AREA)
eqShow = cv.resize(eq,(eq.shape[1] // 6,eq.shape[0] // 6),interpolation=cv.INTER_AREA)
cv.imshow("Original",grayShow)
cv.imshow("Histogram Equalization",eqShow)
cv.waitKey(0)


image = cv.imread("pic/29.png")
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)

plt.figure()
hist = cv.calcHist([gray],[0],None,[256],[0,256])
plt.title("Histogram Grayscale")
plt.xlabel("Bins")
plt.ylabel("Pixels")
plt.plot(hist)
plt.xlabel([0,256])
plt.show()

channels = cv.split(image)
color = ("B","G","R")
plt.title("Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")

for channel,color in zip(channels,color):
    hist = cv.calcHist([channel],[0],None,[256],[0,256])
    plt.plot(hist,color=color)
    plt.xlim([0,256])
plt.figure()
plt.show()

fig = plt.figure()
ax = fig.add_subplot(133)
hist = cv.calcHist([channels[0],channels[2]],[0,1],None,[32,32],[0,256,0,256])
p = ax.imshow(hist,interpolation="nearest")
ax.set_title("2D Color Histogram for B and R")
plt.colorbar(p)
plt.show()


eq = cv.equalizeHist(gray)
print("Pixel = {}".format(eq[272,146]))



fig = plt.figure()
ax = fig.add_subplot(144)
hist = cv.calcHist([image],[0,1,2],None,[9,16,8],[0,256,0,256,0,256])
print("3D histogram shape: {},with {} values".format(hist.shape,hist.flatten().shape[0]))
plt.figure()
plt.axis("off")
plt.imshow(imutils.opencv2matplotlib(image))
plt.show()


























